<template>
  <a-spin :spinning="confirmLoading">
    <a-form ref="formRef" class="antd-modal-form" :labelCol="labelCol" :wrapperCol="wrapperCol">
      <a-row>
        <a-col :span="24">
          <a-form-item label="任务名称333" v-bind="validateInfos.taskName">
            <a-input v-model:value="formData.taskName" placeholder="请输入任务名称" :disabled="true"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="任务开始日期" v-bind="validateInfos.taskStartTime">
		        <a-date-picker placeholder="请选择任务开始日期" v-model:value="formData.taskStartTime" value-format="YYYY-MM-DD"  style="width: 100%" :disabled="true"/>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="任务结束日期" v-bind="validateInfos.taskEndTime">
		        <a-date-picker placeholder="请选择任务结束日期" v-model:value="formData.taskEndTime" value-format="YYYY-MM-DD"  style="width: 100%" :disabled="true"/>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="任务类型" v-bind="validateInfos.taskTypeName">
            <a-input v-model:value="formData.taskTypeName" placeholder="请输入任务类型" :disabled="true"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="任务描述" v-bind="validateInfos.taskContent">
	          <a-textarea v-model:value="formData.taskContent" :rows="4" placeholder="请输入任务描述" :disabled="true"/>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="检查类型" v-bind="validateInfos.inspectTypeName">
	          <j-dict-select-tag v-model:value="formData.inspectTypeName" dictCode="baseCheckType" placeholder="请选择检查类型" :disabled="true"/>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="完成时间" v-bind="validateInfos.finshTime">
		        <a-date-picker placeholder="请选择完成时间" v-model:value="formData.finshTime" value-format="YYYY-MM-DD"  style="width: 100%" :disabled="true"/>
          </a-form-item>
        </a-col>
        <!-- <a-col :span="24">
          <a-form-item label="分解标识" v-bind="validateInfos.salvaged">
            <j-dict-select-tag type='radio' v-model:value="formData.salvaged" dictCode="yn" placeholder="请选择分解标识" :disabled="disabled"/>
          </a-form-item>
        </a-col> -->
        <a-col :span="24">
          <a-form-item label="企业状态" v-bind="validateInfos.enterpriseStatus">
            <j-dict-select-tag type='radio' v-model:value="formData.enterpriseStatus" dictCode="enterpriseStatus" placeholder="请选择企业状态" :disabled="disabled"/>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="是否涉及" v-bind="validateInfos.isInvolves">
            <j-dict-select-tag type='radio' v-model:value="formData.isInvolves" dictCode="isInvolves" placeholder="请选择是否涉及" :disabled="disabled"/>
          </a-form-item>
        </a-col>
      </a-row>
      
      <a-divider v-if="formData.inspectTypeName == '1'">重大危险源检查确认</a-divider>
      <!-- 子表单区域 -->
      <a-tabs defaultActiveKey="1" v-if="formData.inspectTypeName == '1'">
        <a-tab-pane tab="请确认以下危险院已完成检查" key="1">
          <div>
            <a-row type="flex" style="margin-bottom:10px" :gutter="16">
              <a-col :span="4">序号</a-col>
              <a-col :span="10">危险源名称</a-col>
              <a-col :span="4">操作</a-col>
            </a-row>
      
            <a-row type="flex" style="margin-bottom:10px" :gutter="16" v-for="(item, index) in checkPlanList" :key="index">
              <a-col :span="4">
                {{index+1}}
              </a-col>
              <a-col :span="10">
                {{item.hazardName}}
              </a-col>
              <a-col :span="4">
                <a-button type="primary" @click="confirmCompletionInspection(item)" v-if="item.status == 'taskStatus02'">确认已完成检查</a-button>
                <a-button type="dashed" v-if="item.status == 'taskStatus03'" disabled>已确认</a-button>
              </a-col>
            </a-row>
            <a-alert message="每个重大危险源都需要确认检查" type="error" />
          </div>
        </a-tab-pane>
      </a-tabs>
    </a-form>
  </a-spin>
</template>

<script lang="ts" setup>
  import { ref, reactive, defineExpose, nextTick, defineProps, computed, onMounted } from 'vue';
  import { defHttp } from '/@/utils/http/axios';
  import { useMessage } from '/@/hooks/web/useMessage';
  import JDictSelectTag from '/@/components/Form/src/jeecg/components/JDictSelectTag.vue';
  import { getValueType } from '/@/utils';
  import { saveOrUpdate,queryCheckPlanList } from '../TbBaseCheckTask.api';
  import { editStatusComplete } from '../TbBaseCheckPlan.api';
  import { Form } from 'ant-design-vue';
  
  const props = defineProps({
    formDisabled: { type: Boolean, default: false },
    formData: { type: Object, default: ()=>{} },
    formBpm: { type: Boolean, default: true }
  });
  const formRef = ref();
  const useForm = Form.useForm;
  const emit = defineEmits(['register', 'ok']);
  const formData = reactive<Record<string, any>>({
    id: '',
    taskName: '',   
    taskStartTime: '',   
    taskEndTime: '',   
    taskTypeName: '',   
    taskContent: '',   
    inspectTypeName: '',   
    finshTime: '',   
    salvaged: '',   
    enterpriseStatus: '',   
    isInvolves: '',   
  });
  const { createMessage } = useMessage();
  const labelCol = ref<any>({ xs: { span: 24 }, sm: { span: 5 } });
  const wrapperCol = ref<any>({ xs: { span: 24 }, sm: { span: 16 } });
  const confirmLoading = ref<boolean>(false);
  //表单验证
  const validatorRules = {
    taskTypeName: [{ required: true, message: '请输入任务类型!'},],
  };
  const { resetFields, validate, validateInfos } = useForm(formData, validatorRules, { immediate: true });
  const checkPlanList = ref([]);

  // 表单禁用
  const disabled = computed(()=>{
    if(props.formBpm === true){
      if(props.formData.disabled === false){
        return false;
      }else{
        return true;
      }
    }
    return props.formDisabled;
  });

  /**
   * 编辑
   */
  function edit(record) {
    nextTick(() => {
      resetFields();
      //赋值
      Object.assign(formData, record);
      initCheckPlanData();
    });
  }
  
  function initCheckPlanData(){
    let taskParams = {taskId: formData.id,deleted:'0'}
    queryCheckPlanList(taskParams).then((responseResult) => {
      checkPlanList.value = responseResult.records
    })
  }
  
  
  function confirmCompletionInspection(record){
    record.status = "taskStatus03";
    editStatusComplete(record).then((responseResult) => {
      initCheckPlanData()
    })
  }

  /**
   * 提交数据
   */
  async function submitForm() {
    // 触发表单验证
    await validate();
    confirmLoading.value = true;
    const isUpdate = ref<boolean>(false);
    //时间格式化
    let model = formData;
    if (model.id) {
      isUpdate.value = true;
    }
    //循环数据
    for (let data in model) {
      //如果该数据是数组并且是字符串类型
      if (model[data] instanceof Array) {
        let valueType = getValueType(formRef.value.getProps, data);
        //如果是字符串类型的需要变成以逗号分割的字符串
        if (valueType === 'string') {
          model[data] = model[data].join(',');
        }
      }
    }
    await saveOrUpdate(model, isUpdate.value)
      .then((res) => {
        if (res.success) {
          createMessage.success(res.message);
          emit('ok');
        } else {
          createMessage.warning(res.message);
        }
      })
      .finally(() => {
        confirmLoading.value = false;
      });
  }


  defineExpose({
    edit,
    submitForm,
  });
</script>

<style lang="less" scoped>
  .antd-modal-form {
    height: 500px !important;
    overflow-y: auto;
    padding: 14px;
  }
</style>
